package com.szboanda.hbdc.gwgl.fwgl.controller;


import com.szboanda.beijing.pure.BaseBusinessController;
import com.szboanda.hbdc.gwgl.fwgl.service.IGwglFwglService;
import com.szboanda.platform.common.utils.IDGenerator;
import com.szboanda.platform.common.utils.MapUtils;
import com.szboanda.platform.common.utils.StringUtils;
import com.szboanda.platform.file.service.IFileService;
import com.szboanda.platform.v3.util.helper.ActionHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @title: 公文管理
 * @fileName: FwglWorkflowMotionController.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: 原晋川
 * @date: 2019/9/17 10:20
 * @version: V1.0
 */
@Controller
@RequestMapping("/szboanda/hbdc/gwgl/fwgl/controller/fwglworkflowmotioncontroller")
public class FwglWorkflowMotionController extends BaseBusinessController {

    /**
     * 文件操作接口
     */
    @Autowired
    private IFileService fileService;

    /***
     * 注入公文查询接口
     */
    @Autowired
    private IGwglFwglService gwglFwglService;


    /**
     * 功能描述: 跳转至生成文号页面
     *
     * @param
     * @return
     * @throws
     * @auther 梁时鸣
     * @date 2019/9/17 10:50
     */
    @RequestMapping("/skipGenerateFileIdPage")
    public String skipGenerateFileIdPage(@RequestParam("businessId") String businessId, String lclx, ModelMap mp) {
        mp.put("XH", businessId);
        mp.put("LCLX", StringUtils.isEmpty(lclx) ? "FW" : lclx);
        return "/hbdc/gwgl/fwgl/FwglWhglWhsz";
    }


    /**
     * 功能描述: 跳转至生占用添加页面
     *
     * @param
     * @return
     * @throws
     * @auther 梁时鸣
     * @date 2021/3/2 10:50
     */
    @RequestMapping("/AddWhzy")
    public String AddWhzy() {
        return "/hbdc/gwgl/fwgl/AddWhzy";
    }

    /**
     * 功能描述: 查询当前文号
     *
     * @param
     * @return
     * @throws
     * @auther 梁时鸣
     * @date 2021/3/2 10:50
     */
    @ResponseBody
    @RequestMapping("/getFileId")
    public Map<String, Object> getFileId(@RequestBody Map<String, Object> model) {
        String lclx = MapUtils.getString(model, "LCLX");
        String xtbhqz = "发文文号_";
        if (StringUtils.isEmpty(lclx) || "FW".equalsIgnoreCase(lclx)) {
            model = gwglFwglService.getFwInfoByXh(model);
        }
        String wh = MapUtils.getString(model, "WH");
        if (StringUtils.isEmpty(wh)) {
            String whlx = MapUtils.getString(model, "WHLX");
            if (!StringUtils.isEmpty(whlx)) {
                wh = IDGenerator.getInstance().generate("", xtbhqz+whlx, false);
            }
        }
        model.put("LCLX", lclx);
        model.put("WH", wh);
        return model;
    }

    /**
     * 功能描述: 生成文号
     *
     * @param
     * @return
     * @throws
     * @auther 原晋川
     * @date 2019/9/17 10:41
     */
    @ResponseBody
    @RequestMapping("/generateFileId")
    public Map<String, Object> generateFileId(@RequestBody Map<String, Object> model) {
        String lclx = MapUtils.getString(model, "LCLX");
        String newWh = MapUtils.getString(model, "WH");
        String xtbhqz = "发文文号_";
        int i = 0;
        Map<String, Object> searchModel = new HashMap<>();
        searchModel.put("XH", MapUtils.getString(model, "XH"));
        if (StringUtils.isEmpty(lclx) || "FW".equalsIgnoreCase(lclx)) {
            Map<String, Object> oldInfo = gwglFwglService.getFwInfoByXh(searchModel);
            //if (StringUtils.isEmpty(wh)) {
                String whlx = MapUtils.getString(oldInfo, "WHLX");
                if (!StringUtils.isEmpty(whlx)) {
                    String generateWh = IDGenerator.getInstance().generate("", xtbhqz+whlx, true);
                    if (!generateWh.equals(newWh)) {
                        IDGenerator.getInstance().updateSequenceCurrentValue("", xtbhqz+whlx, newWh);
                    }
                }
                searchModel.clear();
                searchModel.put("WH", newWh);
                Map<String, Object> exitsInfo = gwglFwglService.getFwInfoByXh(searchModel);
                if (exitsInfo == null || exitsInfo.isEmpty()) {
                    i = gwglFwglService.saveGwglFwgl(model);
                } else {
                    i = -1;
                }
           /* } else {
                i = 1;
            }*/
        }

        if (i == -1) {
            model.put("error", "文号已被占用");
            return model;
        }
        if (i <= 0) {
            model.put("error", "保存失败");
            return model;
        }
        model.put("success", "保存成功");
        return model;
    }


    /**
     * 根据发文主键获取附件信息
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/getZwFileByXh")
    public Map<String, Object> getZwFileByXh(@RequestBody Map<String, Object> map) {
        Map<String, Object> zwFile = gwglFwglService.getZwFileByXh(map);
        boolean newFlag = MapUtils.getBoolean(map, "NEWFLAG");
        if (newFlag && (zwFile == null || zwFile.isEmpty())) {
            XWPFDocument document = new XWPFDocument();
            File tempFile = new File(MapUtils.getString(map, "WJMC") + ".docx");
            try (FileOutputStream out = new FileOutputStream(tempFile)) {
                document.write(out);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Map<String, Object> fileInfo = new HashMap<>(16);
            fileInfo.put("LXDM", "FWFJ");
            fileInfo.put("ZLXDM", "ZW");
            fileInfo.put("YWSJID", MapUtils.getString(map, "YWSJID"));
            fileInfo.put("WJMC", MapUtils.getString(map, "WJMC") + ".docx");
            fileInfo.put("WJLX", "docx");
            fileInfo.put("WJDX", tempFile.length() / 1024);
            fileInfo.put("SCRID", ActionHelper.getShareId());
            fileInfo.put("SCR", ActionHelper.getShareName());
            fileInfo.put("SCSJ", new Date());
            fileInfo.put("XZCS", 0);
            fileInfo.put("SFSC", 0);
            try (FileInputStream inputStream = new FileInputStream(tempFile)) {
                zwFile = fileService.addFile(inputStream, fileInfo);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return zwFile;
    }

}
